import Util from './util'
import Config from './config'
import Store from '../../store/index'
class Login {
	constructor() {
		this.locked = false
		this.preventRegister = false
	}
	getHeader(opts) {
		if (opts?.preventRegister) {
			this.preventRegister = true
			// setTimeout(()=> {
			// 	this.preventRegister = false
			// }, 1000)
		}
		return new Promise(async (resolve, reject) => {
			try {
				const expiredTime = uni.getStorageSync('expiredTime') || 0
				let now = Math.floor(Date.now() / 1000)
				// 过期，刷新token
				if (!expiredTime || now > expiredTime) {
					await this.login()
				}
				const TOKEN = uni.getStorageSync('token')
				const MID = uni.getStorageSync('mid')
				if (TOKEN && MID) {
					resolve({
						'ACCESS-HEAD-TOKEN': TOKEN,
						'ACCESS-HEAD-MID': MID
					})
				} else {
					reject('get header fail!')
				}
			} catch (err) {
				reject(err)
			}
		})
	}
	/**
	 * 通过code自动登录
	 */
	login() {
		return new Promise((resolve, reject) => {
			if (this.locked) {
				reject()
				return
			}
			this.locked = true
			setTimeout(() => {
				this.locked = false
			}, 500)

			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					let code = loginRes.code
					Util.request({
						url: '/api/loginByCode',
						data: {
							code
						},
						method: 'POST',
						hideToast: true
					}).then(({
						data
					}) => {
						console.log('login:' + JSON.stringify(data))
						uni.setStorageSync('token', data.token)
						uni.setStorageSync('expiredTime', data.expiredTime)
						uni.setStorageSync('mid', data.mid)
						resolve('login success!')
					}).catch(err => {
						if (!this.preventRegister) {
							uni.navigateTo({
								url: '/pages/login/index'
							})
						}
						this.preventRegister = false
						reject()
					})
				}
			});
		})
	}
	/**
	 * 通过微信绑定手机号码登录
	 * 将按钮获取的
	 */
	loginUseWxBindPhone(e) {

	}
	/**
	 * 通过code获取openid
	 */
	getOpenId() {
		return new Promise((resolve, reject) => {
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					let code = loginRes.code
					Util.request({
						url: '/api/wx/getOpenidByCode',
						data: {
							code
						},
						hideToast: true
					}).then(({
						data
					}) => {
						data = JSON.parse(data)
						resolve(data.openid)
					})
				}
			});
		})
	}
	/**
	 * 获取用户信息
	 */
	getUserInfo() {
		return new Promise((resolve, reject) => {
			Util.request({
				url: '/api/user/getUserInfo',
				header: true,
				hideToast: true
			}).then(({
				data
			}) => {
				resolve(data)
			})
		})
	}
	/**
	 * 检查用户信息(布料项目特定)
	 */
	checkUserInfo() {
		return new Promise((resolve, reject) => {
			if (!Store.state.userInfo.userId) {
				Store.dispatch('updateUserInfo').then(userInfo => {
					resolve(userInfo)
				})
			} else {
				resolve(Store.state.userInfo)
			}
		})
	}
}

export default new Login()
